package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.terren.spider.entity.core.Entry;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

public class DizoScheme extends BasicCustomScheme{
	
	 /**//*[@id="pagination"]/div/table/tbody/tr/td[13]/a
	  * 抓分页http://search.58che.com/doc.php?keyword=%C6%FB%B3%B5&order=&page=2
	  */
	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		List<String> pgList = new ArrayList<String>();// 存放分页list
		boolean flag = false;
		String nextUrl = null;
		String upUrl = null;
		List<String> pageUrls = page.getHtml().xpath("//*[@id='pagination']").links().all();
		Selectable selectable= page.getHtml().xpath("//*[@id='pagination']");
		List<String> urlDivs = selectable.xpath("//a").all();
		for(String urlDiv:urlDivs){
			Html childHtml = Html.create(urlDiv);//将找到的div字符串转为html
			String pageText = childHtml.xpath("//a/text()").get();
			String pageUrl = childHtml.css("a","href").get();
			if("下一页".equals(pageText)&&pageUrl!=null){
				nextUrl = "http://www.dizo.com.cn"+pageUrl;
			}
			if("上一页".equals(childHtml.xpath("//a/text()").get())&&childHtml.css("a","href").get()!=null){
				upUrl = "http://www.dizo.com.cn"+pageUrl;
			}
		}

		if (nextUrl != null) {//抓【下一页】链接
			pgList.add(nextUrl);
		}
		if (upUrl != null || nextUrl !=null) {//包含上一页或者下一页链接
			flag = true;
		}
		if (!flag) {//没有上一页链接也没有下一页链接 ，取全部
			pgList.addAll(pageUrls);
		}
		//pageUrls.addAll(pageUrls);
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(pgList);
		return resultSet;
	}	
}
